﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace project.B12.application.Views.Control
{
    /// <summary>
    /// ImageControl.xaml 的交互逻辑
    /// </summary>
    public partial class ImageControl : UserControl
    {
        public ImageControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 鼠标是否按下
        /// </summary>
        private bool mouseDown;

        /// <summary>
        /// 移动图片前，按下鼠标左键时，鼠标相对于Part_ImageContainer的点
        /// </summary>
        Point moveStart;


        // 鼠标左键按下
        private void ImgMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Part_ImageContainer.CaptureMouse();
            mouseDown = true;
            moveStart = e.GetPosition(Part_ImageContainer);
        }

        // 鼠标左键松开
        private void ImgMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Part_ImageContainer.ReleaseMouseCapture();
            mouseDown = false;
        }

        /// <summary>
        /// 移动图片
        /// </summary>
        /// <param name="moveEndPoint">移动图片的终点（相对于Part_ImageContainer）</param>
        private void DoMove(Point moveEndPoint)
        {
            Point start = Part_ImageContainer.TranslatePoint(moveStart, Part_Image);
            Point end = Part_ImageContainer.TranslatePoint(moveEndPoint, Part_Image);
            if (scaler.ScaleX > 7)
            {
                transer.X += (end.X - start.X) * 4;
                transer.Y += (end.Y - start.Y) * 4;
            }
            else if (scaler.ScaleX > 5)
            {
                transer.X += (end.X - start.X) * 3;
                transer.Y += (end.Y - start.Y) * 3;
            }
            else if (scaler.ScaleX > 3)
            {
                transer.X += (end.X - start.X) * 2;
                transer.Y += (end.Y - start.Y) * 2;
            }
            else if (scaler.ScaleX < 0.5)
            {
                transer.X += (end.X - start.X) * 0.5;
                transer.Y += (end.Y - start.Y) * 0.5;
            }
            else
            {
                transer.X += (end.X - start.X);
                transer.Y += (end.Y - start.Y);
            }
            moveStart = moveEndPoint;

            if (transer.X * 2 > Part_Image.ActualWidth + Part_ImageContainer.ActualWidth - 20)
                transer.X = (Part_Image.ActualWidth + Part_ImageContainer.ActualWidth - 20) / 2;

            if (-transer.X * 2 > (2 * scaler.ScaleX - 1) * Part_Image.ActualWidth + Part_ImageContainer.ActualWidth - 20)
                transer.X = -((scaler.ScaleX - 0.5) * Part_Image.ActualWidth + Part_ImageContainer.ActualWidth / 2 - 10);

            if (transer.Y * 2 > Part_Image.ActualHeight + Part_ImageContainer.ActualHeight - 20)
                transer.Y = (Part_Image.ActualHeight + Part_ImageContainer.ActualHeight - 20) / 2;

            if (-transer.Y * 2 > (2 * scaler.ScaleY - 1) * Part_Image.ActualHeight + Part_ImageContainer.ActualHeight - 20)
                transer.Y = -((scaler.ScaleY - 0.5) * Part_Image.ActualHeight + Part_ImageContainer.ActualHeight / 2 - 10);
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImgMouseMove(object sender, MouseEventArgs e)
        {
            var mouseEnd = e.GetPosition(Part_ImageContainer);// 鼠标移动时，获取鼠标相对图片容器的点
            if (mouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)// 按下鼠标左键，移动图片
                {
                    DoMove(mouseEnd);
                }
            }
        }

        // 鼠标滚轮滚动
        private void ImgMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var point = e.GetPosition(Part_Image);
            var delta = e.Delta * 0.002;
            DoScale(point, delta);
        }

        /// <summary>
        /// 缩放图片。最小为0.1倍，最大为30倍
        /// </summary>
        /// <param name="point">相对于图片的点，以此点为中心缩放</param>
        /// <param name="delta">缩放的倍数增量</param>
        private void DoScale(Point point, double delta)
        {
            // 限制最大、最小缩放倍数
            if (scaler.ScaleX + delta < 0.1 || scaler.ScaleX + delta > 30) return;

            scaler.ScaleX += delta;
            scaler.ScaleY += delta;

            transer.X -= point.X * delta;
            transer.Y -= point.Y * delta;
        }

    }
}
